package com.doit.day03

import java.sql.{Connection, DriverManager, PreparedStatement}

object _04_算子 {
  def main(args: Array[String]): Unit = {
    val list: List[String] = List("hello,hi,hello","hello,hi,hello,hello")

    val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "123456")
    val pps: PreparedStatement = conn.prepareStatement("insert into abc values(?)")
    //将数据写入到mysql中
    list.foreach((str:String)=>{
      val str1: String = str.toUpperCase()
      pps.setString(1,str1)
      pps.execute()
      println(str)
    })


    //进去的是什么
    //出来的是什么
    //不管到了哪一步，现在你写的这个变量是什么类型
    /*

    //map操作:对集合中的每一个元素进行操作，原来集合中有多少个元素，结果集合中就有多少个元素
    val res: List[String] = list.map((a: Int) => {
      //函数体里面做的事情就是你想对元素做的操作
      val str: String = a * a * a + 10 - 20 + "hello"
      println(str)
      str
    })

    res.foreach((a:String)=>{
      val str: String = a + "加个啥嘞"
      println(str)
    })*/
//    List("hello","hi","hello","hello","hi","hello","hello")
    val list1 :List[Array[String]] = list.map((line: String) => {
      val arr: Array[String] = line.split(",")
      arr
    })
    // List(Array("hello","hi","hello"),Array("hello","hi","hello","hello"))
    val flatten: List[String] = list1.flatten
    println(flatten)

    //flatMap
    println(list.flatMap((line: String) => {
      line.split(",")
    }))


    //sum
    val list2: List[Int] = List(2, 2, 3, 2, 3, 6)
    val sum: Int = list2.sum
    val cnt: Int = list2.count((i: Int) => i%2 ==0)
    println(sum)
    println(cnt)
    //grouped  ==》 没啥用
//    val iterator: Iterator[List[Int]] = list2.grouped(4)
//    iterator.foreach((a:List[Int])=>{println(a)})

    //groupBy ==》 和sql中的group by 一样
    //Map(2 -> List(2, 2, 2), 3 -> List(3, 3), 6 -> List(6))
    val map: Map[Int, List[Int]] = list2.groupBy((word: Int) => word)
    println(map)

    //reduce  reduceLeft   reduceRight
    val i: Int = list2.reduce((a: Int, b: Int) => {a - b})
    println(i)//18  -14

    println(list2.reduceRight((a: Int, b: Int) => {
      a - b
    })) //-2

    //fold    foldLeft   foldRight
    val i1: Int = list2.fold(10)((a: Int, b: Int) => a + b)
    println(i1)

















  }

}
